//
//  FirstViewController.m
//  FunOCR
//
//  Created by Luke Du on 8/03/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "HomeViewController.h"


#import "PhotoViewController.h"

#import "FunOCRAppDelegate.h"

#import "PlistHandle.h"

#import "Reachability.h"

@implementation HomeViewController
@synthesize takePictureButton; 


- (void)dealloc {

	[takePictureButton release]; 
	
	[super dealloc];
}





- (void)viewDidLoad {
	//if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { 
//		takePictureButton.hidden = YES; 
//	}
	
	appDelegate = (FunOCRAppDelegate *)[[UIApplication sharedApplication] delegate];			
	PlistHandle *plistHandler = [[PlistHandle alloc]initWithName:@"setting.plist"];
	appDelegate.setting = [plistHandler readContent];
	if(([appDelegate.setting objectForKey:@"username"]!=nil)&&([appDelegate.setting objectForKey:@"password"]!=nil)){
	}
	else{
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Settings aren't available." 
							  message:@"Google account details are needed to perform OCR service, tap settings to set up your account details." 
							  delegate:self cancelButtonTitle:@"Not now" otherButtonTitles:nil];
		[alert addButtonWithTitle:@"Settings"]; 
		[alert show];
		[alert autorelease];
	}
	[plistHandler release];	
	
	
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex != [alertView cancelButtonIndex]){
		[self showSettingsViewController];
	}
		
}


- (void)viewDidUnload {
	// Release any retained subviews of the main view. // e.g. self.myOutlet = nil; 
	self.takePictureButton = nil; 
	[super viewDidUnload];
}





#pragma mark - 
- (IBAction)getCameraPicture:(id)sender {
	
	appDelegate = (FunOCRAppDelegate *)[[UIApplication sharedApplication] delegate];			
	if(![[appDelegate.setting objectForKey:@"username"] isEqualToString:@""]&&![[appDelegate.setting objectForKey:@"password"] isEqualToString:@""]){
		UIImagePickerController *picker = [[UIImagePickerController alloc] init];
		picker.delegate = self; 
		//picker.allowsEditing = YES; 
		picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary; 
		[self presentModalViewController:picker animated:YES]; 
		[picker release];
	}
	else{
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Settings aren't available." 
							  message:@"Google account details are needed to perform OCR service, tap settings to set up your account details." 
							  delegate:self cancelButtonTitle:@"Not now" otherButtonTitles:nil];
		[alert addButtonWithTitle:@"Settings"]; 
		[alert show];
		[alert autorelease];
	}
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	
	NSLog(@"info is %@",info);
	
	[picker dismissModalViewControllerAnimated:NO];
		
	[self showPhotoViewController:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];

}





-(void)showPhotoViewController:(UIImage *) image{
	PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
	
	[photoViewController setImage:image];
	
	
	[self presentModalViewController:photoViewController animated:YES];
	
	
	[photoViewController release];
}





-(void)showSettingsViewController{
	self.tabBarController.selectedIndex = 1;
}




- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[picker dismissModalViewControllerAnimated:YES];

}


@end